﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using UnityEngine;
using System.Net;
using Google.Protobuf;

public enum EMessageType
{
    NONE,
    HELLO,
    ACK,
    PING,
    C2S_JOIN,
    C2S_SYNC,
    S2C_QUIT,
    S2C_JOIN,
    S2C_SYNC,
    S2C_SYNC_AI,
    S2C_AI_JOIN,
    S2C_AI_JOIN_BATCH,
    C2S_RESEND_TEST,
    S2C_RESEND_TEST,
    C2S_ORDER_TEST,
    S2C_ORDER_TEST,
}

public class MessageManager : TSingleton<MessageManager>
{
    Dictionary<System.Type, ObjectPool<IMessage>> m_MessagePool = new Dictionary<Type, ObjectPool<IMessage>>();

    DateTime m_StartTime;

    public void Init()
    {
        m_StartTime = DateTime.Now;
    }

    public IMessage Get(System.Type type)
    {
        if (!m_MessagePool.ContainsKey(type))
        {
            m_MessagePool.Add(type, new ObjectPool<IMessage>(type));
        }
        return m_MessagePool[type].Get();
    }

    public T Get<T>()
        where T : class, IMessage, new()
    {
        return Get(typeof(T)) as T;
    }

    public void Put(IMessage msg)
    {
        var type = msg.GetType();
        if (!m_MessagePool.ContainsKey(type))
        {
            m_MessagePool.Add(type, new ObjectPool<IMessage>(type));
        }
        m_MessagePool[type].Put(msg);
    }

    public bool IsRemoteIPIdentity(IPEndPoint left, IPEndPoint right)
    {
        return left.Address.Equals(right.Address) && left.Port == right.Port;
    }

    public float GetCurElapsedTime()
    {
        return (float)(System.DateTime.Now - m_StartTime).TotalSeconds;
    }

    Dictionary<EMessageType, System.Type> m_MsgDict = new Dictionary<EMessageType, System.Type>()
        {
            { EMessageType.HELLO, typeof(Hello)},
            { EMessageType.ACK, typeof(Ack)},
            { EMessageType.PING, typeof(Ping)},
//#if SERVER
            { EMessageType.C2S_JOIN, typeof(C2S_PlayerJoin)},
            { EMessageType.C2S_SYNC, typeof(C2S_PlayerSync)},
            { EMessageType.S2C_JOIN, typeof(S2C_PlayerJoin)},
            { EMessageType.S2C_SYNC, typeof(S2C_PlayerSync)},
            { EMessageType.S2C_SYNC_AI, typeof(S2C_PlayerSyncAI)},
            { EMessageType.S2C_AI_JOIN, typeof(S2C_AIJoin)},
            { EMessageType.S2C_AI_JOIN_BATCH, typeof(S2C_AIJoinBatch)},
            { EMessageType.C2S_RESEND_TEST, typeof(C2S_ResendTest)},
            { EMessageType.S2C_RESEND_TEST, typeof(S2C_ResendTest)},
            { EMessageType.C2S_ORDER_TEST, typeof(C2S_OrderTest)},
            { EMessageType.S2C_ORDER_TEST, typeof(S2C_OrderTest)},
//#else
//#endif
        };

    public EMessageType GetMessageType(IMessage msg)
    {
        if (msg == null)
        {
            return EMessageType.NONE;
        }

        foreach (var item in m_MsgDict)
        {
            if (item.Value == msg.GetType())
            {
                return item.Key;
            }
        }
        return EMessageType.NONE;
    }

    public System.Type GetMessageSystemType(EMessageType type)
    {
        if (!m_MsgDict.ContainsKey(type))
        {
            return typeof(IMessage);
        }
        return m_MsgDict[type];
    }
}
